
// Copyright (c) 2002 HTHK
package com.ffpc.sql;


import com.ffpc.util.DateFormatter;

import java.text.ParseException;
import java.sql.Timestamp;
import java.io.Serializable;
import java.math.BigDecimal;

/**
 * A Class class.
 * <P>
 * @author HTHK
 */
public class Column implements Serializable
{
    private int index;
    private String name;
    private int type;
    private Serializable value;
    private int size;
    private boolean nullable;
    private boolean invalidInput;

    public static class Types
    {
      public static final int string = java.sql.Types.VARCHAR;
      public static final int number = java.sql.Types.DECIMAL;
      public static final int time = java.sql.Types.TIMESTAMP;
      public static final int integer = java.sql.Types.INTEGER ;
      public static final int time_n = java.sql.Types.TIME ;
    }

    /**
    * Constructor
    */
    public Column()
    {
      this.index = 0;
      this.name = null;
      this.type = 0;
      this.value = null;
      this.size = 0;
      nullable= true;
      invalidInput = true;
    }

    public Column(int index, String name, int type, Serializable value, int size, boolean nullable)
    {
      this.index = index;
      this.name = name;
      this.type = type;
      this.size = size;
      this.nullable = nullable;
      this.setValue(value);
    }

    public void setIndex(int index)
    {
      this.index = index;
    }

    public void setName(String name)
    {
      this.name =  name;
    }

    public void setType(int type)
    {
      this.type = type;
    }

    public void setValue(Serializable obj)
    {
      this.setInvalidInput(false);
      if (obj==null)
      {
          if (this.getNullable())
            this.value=obj;
          else
            this.setInvalidInput(true);
      }
      else if (obj instanceof String)
      {
          if (this.getType()==Types.string)
          {
              if (((String)obj).trim().length()>this.getSize())
                  this.setInvalidInput(true);
              else
                  this.value = obj;
          }

          else if (this.getType()==Types.integer )
          {
              try
              {
                  this.value = new Integer((String)obj);
              }
              catch (NumberFormatException e)
              {
                  this.setInvalidInput(true);
              }
          }
          else if (this.getType()==Types.number)
          {
              try
              {
                  this.value = new BigDecimal((String)obj);
              }
              catch (NumberFormatException e)
              {
                  this.setInvalidInput(true);
              }
          }
          else if (this.getType()==Types.time)
          {
              try
              {
                   this.value = DateFormatter.parse((String)obj);
              }
              catch (ParseException e)
              {
                   this.setInvalidInput(true);
              }
          }
          else if (this.getType()==Types.time_n)
          {
              try
              {
                   this.value = DateFormatter.parse_n((String)obj);
              }
              catch (ParseException e)
              {
                   this.setInvalidInput(true);
              }
          }
          else
          {
              this.setInvalidInput(true);
          }
      }
      else if ( (obj instanceof Integer) || (obj instanceof Double) || (obj instanceof Timestamp))
      {
          this.value = obj;
      }
      else
      {
          this.setInvalidInput(true);
      }
    }

    @Override
	public String toString()
    {
        if (this.getType()==Column.Types.string)
          return (String)this.getValue();
        if (this.getType()==Column.Types.number)
          return ((Double)this.getValue()).toString();
        //
        if (this.getType()==Column.Types.integer )
          return ((Integer)this.getValue()).toString() ;
        //
        if (this.getType()==Column.Types.time)
          return DateFormatter.format((Timestamp)this.getValue());
        else
        if (this.getType()==Column.Types.time_n)
          return DateFormatter.format_n((Timestamp)this.getValue());
        else
          return "";
    }

    public void setSize(int size)
    {
      this.size = size;
    }

    public void setNullable(boolean nullable)
    {
      this.nullable = nullable;
    }

    private void setInvalidInput(boolean invalidInput)
    {
      this.invalidInput = invalidInput;
    }

    public int getIndex()
    {
      return this.index;
    }

    public String getName()
    {
      return this.name;
    }

    public int getType()
    {
      return this.type;
    }

    public Serializable getValue()
    {
      return this.value;
    }

    public int getSize()
    {
      return this.size;
    }

    public boolean getNullable()
    {
      return this.nullable;
    }

    public boolean getInvalidInput()
    {
      return this.invalidInput;
    }

    public Column clone(Serializable value)
    {
      return new Column(this.index,new String(name),this.type,value,this.size,this.nullable);
    }
}


